%%
%% This is file `totcount.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% totcount.dtx  (with options: `sty')
%% 
%% The TotCount package, version 1.0, 2009/04/14
%% 
%% Copyright (c) [2009] Vasileios Koutavas (Vasileios.Koutavas@cs.tcd.ie)
%% 
%% This program may be re-distributed and/or modified under the terms of the
%% LaTeX Project Public License version 1.3c, or any later version.
%% The latest version of this license is in
%%    CTAN:macros/latex/base/lppl.txt
%% 
\def\totcfileversion{1.1}
\def\totcfiledate{2009/07/09}
\def\totcdocdate{9 Jul 2009}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{totcount}
  [\totcfiledate \space v\totcfileversion \space package for getting%
   the total value of LaTeX counters]
%%
%%   Import of the \xpackage{keyval} package \cite{keyval}:
\RequirePackage{keyval}
%%
%%   Creates a new counter and registers it as a total counter. This is the
%%   top-level dispatch of the macro, depending on whether there is an optional
%%   argument or not.
\def\newtotcounter{%
  \@ifnextchar[\newtotcounter@newaux\newtotcounter@mainaux}
%%
%%   This is the version of the |\newtotcounter| macro that uses a separate
%%   auxiliary file. It first creates the counter (second argument) and then
%%   calls the macro |\regtotcounter|.
\def\newtotcounter@newaux[#1]#2{%
  \newcounter{#2}%
  \regtotcounter[#1]{#2}%
}
%%
%%   This is the version of the |\newtotcounter| macro that uses the main
%%   auxiliary file.  It first creates the counter (argument) and then calls
%%   the macro |\regtotcounter|.
\def\newtotcounter@mainaux#1{%
  \newcounter{#1}%
%% Register the counter:
  \regtotcounter{#1}%
}
%%
%%   Registers a counter as a total counter. This is the top-level dispatch of
%%   the macro, depending on whether there is an optional argument or not.
\def\regtotcounter{%
  \@ifnextchar[\regtotcounter@newaux\regtotcounter@mainaux}
%%
%%   The following is the definition of the |auxfile| key for specifying an
%%   alternative auxiliary file when calling the macro |\regtotcounter| (see
%%   \cite{keyval}).
\define@key{totcounter}{auxfile}{\def\this@auxfile{#1}}
%%
%%   This is the version of the |\regtotcounter| macro that uses a separate
%%   auxiliary file. The auxiliary file is passed as a first argument in the
%%   form of a key--value pair |[auxfile=|\meta{file}|]|, and the counter to be
%%   registered is passed as the second argument.
%%   \begin{macrocode}
\def\regtotcounter@newaux[#1]#2{%
  \setkeys{totcounter}{#1}%
%% Try to load the contents of the file:
  \InputIfFileExists{\this@auxfile}{}{}%
%% Make sure that the auxiliary file is open; \LaTeX\ will close it at the end:
  \expandafter\ifx\csname \this@auxfile @open\endcsname\relax%
    \expandafter\gdef\csname \this@auxfile @open\endcsname{}%
    \expandafter\newwrite\csname \this@auxfile @stream\endcsname%
    \immediate\expandafter\openout%
      \csname \this@auxfile @stream\endcsname=\this@auxfile%
  \fi%
%% Create a new counter holding the total number of the actual counter:
  \expandafter\ifx\csname c@#2@totc\endcsname\relax%
      \newcounter{#2@totc}%
      \setcounter{#2@totc}{-1}%
  \fi%
%% At the end of the document write code in the auxiliary file to update the
%% total counter with the value of the actual counter:
  \AtEndDocument{%
     \def\sp{ }%
     \immediate\expandafter\write%
     \csname \this@auxfile @stream\endcsname{%
       \string\expandafter\string\ifx%
         \string\csname\sp c@#2@totc\string\endcsname\string\relax%
       \string\newcounter{#2@totc}%
       \string\fi%
       \string\setcounter{#2@totc}{\number\value{#2}}%
     }%
  }%
}
%%
%%   This is the version of the |\regtotcounter| macro that uses the main
%%   auxiliary file. The counter to be registered is passed as the second
%%   argument.
\def\regtotcounter@mainaux#1{%
%% Create a new counter holding the total number of the actual counter:
  \expandafter\ifx\csname c@#1@totc\endcsname\relax%
      \newcounter{#1@totc}%
      \setcounter{#1@totc}{-1}%
  \fi%
%% At the end of the document write code in the auxiliary file to update the
%% total counter with the value of the actual counter:
  \AtEndDocument{%
    \def\sp{ }%
    \immediate\write\@mainaux{%
      \string\expandafter\string\ifx%
        \string\csname\sp c@#1@totc\string\endcsname\string\relax%
      \string\newcounter{\string #1@totc}%
      \string\fi%
      \string\setcounter{\string #1@totc}{\number\value{#1}}%
    }%
  }%
}
%%
%%   Prints the total value of a registered total counter that is passed as
%%   argument. If the total value is yet to be computed (at the first time
%%   \LaTeX\ runs on the document) then its value is |-1| and the output of the
%%   command is $??$.
\newcommand\total[1]{%
  \def\tmp@val{\value{#1@totc}}%
  \ifnum\tmp@val=-1%
    \message{Rerun to get correct total counts}%
    $??$%
  \else%
    \number\value{#1@totc}%
  \fi%
}
%%
%%   Returns the numeric total value of a registered total counter that is
%%   passed as argument. Note that if the counter's value is not yet computed
%%   (at the first time \LaTeX\ runs on the document) this macro returns |-1|.
\newcommand\totvalue[1]{\value{#1@totc}}
%%
%%   Inputs an auxiliary file that should contain total-counter
%%   definitions. It outputs a warning message in the terminal if the file
%%   doesn't exist. This command should be used \emph{instead} of registering a
%%   counter.
\newcommand\usetotcountfile[1]{%
  \InputIfFileExists{#1}{%
    \message{TotCount inputs file '#1'}%
  }{%
    \message{TotCount Warning: File '#1' does not exist!}%
    \message{\space\space\space\space\space\space\space\space\space%
             \space\space\space\space\space\space\space\space\space%
             Ignoring \string\usetotcountfile{#1}.}%
  }%
}
\endinput
%%
%% End of file `totcount.sty'.
